package com.example.sy3;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResultServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String[] loves = request.getParameterValues("loves");
        String email = request.getParameter("email");
        String tel = request.getParameter("tel");

        // 处理可能为空的情况
        if (name == null || pwd == null || age == null || sex == null || email == null || tel == null) {
            response.sendRedirect("register.html");
            return;
        }

        // 密码长度验证
        if (pwd.length() < 6 || pwd.length() > 10) {
            response.sendRedirect("register.html");
            return;
        }

        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pWriter = response.getWriter();

        pWriter.print("<body bgcolor='pink'>");
        pWriter.print("用户名: " + name + "<br/>");
        pWriter.print("密码: ******<br/>"); // 保护密码
        pWriter.print("年龄: " + age + "<br/>");
        pWriter.print("性别: " + sex + "<br/>");
        pWriter.print("爱好: " + (loves != null ? Arrays.toString(loves) : "无") + "<br/>");
        pWriter.print("Email: " + email + "<br/>");
        pWriter.print("Tel: " + tel + "<br/>");
        pWriter.print("</body>");
    }
}